package cn.yyt.yytravel.guide.service.impl;

import cn.yyt.yytravel.base.exception.ServiceException;
import cn.yyt.yytravel.base.response.StatusCode;
import cn.yyt.yytravel.guide.mapper.GuideMapper;
import cn.yyt.yytravel.guide.pojo.dto.SaveGuideForm;
import cn.yyt.yytravel.guide.pojo.entity.Guide;
import cn.yyt.yytravel.guide.pojo.vo.GuideVO;
import cn.yyt.yytravel.guide.service.GuideService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;

import static cn.yyt.yytravel.base.response.StatusCode.USERNAME_ALREADY_EXISTS;

@Slf4j
@Service
public class GuideServiceImpl implements GuideService {
    @Autowired
    GuideMapper guideMapper;
    
    @Override
    public void saveGuide(SaveGuideForm saveGuideForm) {
        log.debug("数据是？" + saveGuideForm);
        Guide guide = new Guide();
        //属性值赋值
        BeanUtils.copyProperties(saveGuideForm,guide);
        //判断该导游id是否存在
        if (guide.getId()==null){
            //不存在就新增,这里需要用户名是否存在
            if(guideMapper.selectGuideName(guide.getUsername())!=null){
                //抛出异常
                throw new ServiceException(USERNAME_ALREADY_EXISTS);
            }
            guide.setCreateTime(new Date());
            guideMapper.insertGuide(guide);
            
        }else {
            guide.setCreateTime(new Date());
            guideMapper.update(guide);
        }

    }

    @Override
    public List<GuideVO> loadGuide() {
        List<GuideVO> list = guideMapper.loadGuide();
        return list;
    }

    @Override
    public List<GuideVO> selectGuide() {
        List<GuideVO> list = guideMapper.selectGuide();
        return list;
    }
}
